package com.frame.management;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.excel.Utils;
import com.excel.service.StudentService;
import com.excel.service.StudentServiceImpl;
import com.excel.student.Student;
//维护表格
public class MainSystem2 extends JFrame{

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DefaultTableModel tableModel;   //表格模型对象
    private JTable table;
    private JTextField aTextField;
    private JTextField bTextField;
    private JTextField cTextField;
    private StudentService service;
    
    
    private JTextField searchText;

    public static String [][]tableVales ;
    
    public MainSystem2()
    {

        super();
    	setSize(800, 600);
		setLocation(400, 170);
        setTitle("*****欢迎登录宿舍安全卫生检查系统*****");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] columnNames = {"学号","姓名","班级"};   //列名
        
        //得到student service，用来增删改查
        service = StudentServiceImpl.getInstance();
        //初始化Table的每行数据
        tableVales = new String[Utils.studentMap.keySet().size()][];
        List<String[]> list = new ArrayList<>();
        for (String key: Utils.studentMap.keySet()) {
			list.add(Utils.studentMap.get(key).toArray());
		}
        for (int i = 0; i < tableVales.length; i++) {
        	tableVales[i] = list.get(i);
		}
        
        
        tableModel = new DefaultTableModel(tableVales,columnNames);
        table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);   //支持滚动
        getContentPane().add(scrollPane,BorderLayout.CENTER);
        //jdk1.6
        //排序:
        //table.setRowSorter(new TableRowSorter(tableModel));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  //table只能单选
        //单机选中每一行，数据填入到最下边
        table.addMouseListener(new MouseAdapter(){    //鼠标事件
            public void mouseClicked(MouseEvent e){
                int selectedRow = table.getSelectedRow(); //获得选中行索引
                Object oa = tableModel.getValueAt(selectedRow, 0);
                Object ob = tableModel.getValueAt(selectedRow, 1);
                Object oc= tableModel.getValueAt(selectedRow, 2);

                aTextField.setText(oa.toString());  //给文本框赋值
                bTextField.setText(ob.toString());
                cTextField.setText(oc.toString());

            }
        });
        
        scrollPane.setViewportView(table);
        final JPanel panel = new JPanel();
        getContentPane().add(panel,BorderLayout.SOUTH);
        
    }
    
    

}